package com.zrwl.poscloud.xo.service.impl;

import com.zrwl.poscloud.base.constants.TransitConstants;
import com.zrwl.poscloud.base.constants.TransitSendConstants;
import com.zrwl.poscloud.base.entity.GradeTeacherR;
import com.zrwl.poscloud.base.entity.Transit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.entity.TransitSend;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.enums.ETransit;
import com.zrwl.poscloud.base.enums.EUser;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.DateUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.service.*;
import com.zrwl.poscloud.xo.vo.TeacherAdminRVO;
import com.zrwl.poscloud.xo.vo.TransitSendVO;
import com.zrwl.poscloud.xo.vo.UserEmployeeRVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import com.zrwl.poscloud.xo.mapper.TransitMapper;
import com.zrwl.poscloud.xo.vo.TransitVO;

import javax.annotation.Resource;

/**
 * 出校管理 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-03-17
 */
@Service
public class TransitServiceImpl extends SuperServiceImpl<TransitMapper, Transit> implements TransitService {

    @Autowired
    private TransitService transitService;
    @Resource
    private TransitMapper transitMapper;
    @Autowired
    private UserEmployeeRService userEmployeeRService;
    @Autowired
    private TransitSendService transitSendService;
    @Autowired
    private GradeTeacherRService gradeTeacherRService;
    @Autowired
    private TeacherAdminRService teacherAdminRService;

    @Override
    public IPage<TransitVO> getPageList(TransitVO transitVO) {
        Page<Transit> page = new Page<>();
        page.setCurrent(transitVO.getCurrentPage());
        page.setSize(transitVO.getPageSize());
        IPage<TransitVO> pageList = transitMapper.getPageList(page, transitVO);
        List<TransitVO> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public List<TransitVO> getAllList(TransitVO transitVO) {
        List<TransitVO> list = transitMapper.getAllList(transitVO);
        for (TransitVO transit : list) {
            transit.setTypeName(TransitConstants.getTypeName(transit.getType()));
            transit.setStatusName(TransitConstants.getStatusName(transit.getType(), transit.getStatus()));
        }
        return list;
    }

    @Override
    public String add(TransitVO transitVO) {
        Transit entity = new Transit();
        BeanUtils.copyProperties(transitVO, entity, SysConf.STATUS);
        entity.insert();
        transitVO.setUid(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String edit(TransitVO transitVO) {
        Transit entity = transitService.getById(transitVO.getUid());
        BeanUtils.copyProperties(transitVO, entity);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String delete(TransitVO transitVO) {
        Transit entity = transitService.getById(transitVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public TransitVO getLastTimeTransitList(TransitVO transitVO) {
        TransitVO lastTimeEnterSchool = transitMapper.getLastTimeTransit(transitVO, TransitConstants.enter_school);
        if (lastTimeEnterSchool != null) {
            if (lastTimeEnterSchool.getStatus() == TransitConstants.enable) {
                transitVO.setType(lastTimeEnterSchool.getType());
                transitVO.setTypeName(TransitConstants.getTypeName(lastTimeEnterSchool.getType()));
                transitVO.setStatusName(TransitConstants.getStatusName(lastTimeEnterSchool.getType(), lastTimeEnterSchool.getStatus()));
            }
            transitVO.setEnterTime(lastTimeEnterSchool.getSourceTime());
        }
        TransitVO lastTimeOutSchool = transitMapper.getLastTimeTransit(transitVO, TransitConstants.exit_school);
        if (lastTimeOutSchool != null) {
            if (lastTimeOutSchool.getStatus() == TransitConstants.enable) {
                transitVO.setTypeName(TransitConstants.getTypeName(lastTimeOutSchool.getType()));
                transitVO.setType(lastTimeOutSchool.getType());
                transitVO.setStatusName(TransitConstants.getStatusName(lastTimeOutSchool.getType(), lastTimeOutSchool.getStatus()));
            }
            transitVO.setExitTime(lastTimeOutSchool.getSourceTime());
        }
        if(transitVO.getType() == TransitConstants.enter_school){
            if(transitVO.getExitTime() != null){
                transitVO.setIntervalTime(DateUtils.getDatePoor(transitVO.getEnterTime(),transitVO.getExitTime()));
            }else {
                transitVO.setIntervalTime("暂无离校时间");
            }
        }
        if(transitVO.getType() == TransitConstants.exit_school){
            if(transitVO.getEnterTime() != null){
                transitVO.setIntervalTime(DateUtils.getDatePoor(transitVO.getEnterTime(),transitVO.getExitTime()));
            }else {
                transitVO.setIntervalTime("暂无入校时间");
            }
        }
        return transitVO;
    }

    @Override
    public TransitVO getRecentlyHomeSchoolInfo(TransitVO transitVO) {
        TransitVO schoolInfo = transitMapper.getRecentlyHomeSchoolInfo(transitVO);
        schoolInfo.setTypeName(TransitConstants.getTypeName(schoolInfo.getType()));
        schoolInfo.setStatusName(TransitConstants.getStatusName(schoolInfo.getType(), schoolInfo.getStatus()));
        return schoolInfo;
    }

    @Override
    public TransitVO getHomeSchoolInfo(TransitVO transitVO) {
        TransitVO schoolInfo = transitMapper.getHomeSchoolInfo(transitVO);
        schoolInfo.setTypeName(TransitConstants.getTypeName(schoolInfo.getType()));
        schoolInfo.setStatusName(TransitConstants.getStatusName(schoolInfo.getType(), schoolInfo.getStatus()));
        return schoolInfo;
    }

    @Override
    public String handleTransitNoticeParents(TransitVO transitVO) {
        Transit transit = transitService.getById(transitVO.getUid());
        if (transit == null) {
            throw new RuntimeException("离校信息不存在");
        }
        List<UserEmployeeRVO> list = userEmployeeRService.getByEmployeeUid(transit.getEmployeeUid(), 1);
        for (UserEmployeeRVO userEmployeeRVO : list) {
            TransitSendVO transitSendVO = new TransitSendVO();
            transitSendVO.setTransitUid(transit.getUid());
            transitSendVO.setUserUid(userEmployeeRVO.getUserUid());
            transitSendVO.setType(TransitSendConstants.type_parents);
            transitSendService.add(transitSendVO);
        }
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String handleTransitNoticeTeacher(TransitVO transitVO) {
        Transit transit = transitService.getById(transitVO.getUid());
        if (transit == null) {
            throw new RuntimeException("离校信息不存在");
        }
        GradeTeacherR gradeTeacherR = gradeTeacherRService.getGradeTeacherByEmployeeUid(transit.getEmployeeUid(), 0);
        List<UserEmployeeRVO> list = userEmployeeRService.getByEmployeeUid(gradeTeacherR.getEmployeeUid(), 0);
        for (UserEmployeeRVO userEmployeeRVO : list) {
            TransitSendVO transitSendVO = new TransitSendVO();
            transitSendVO.setTransitUid(transit.getUid());
            transitSendVO.setUserUid(userEmployeeRVO.getUserUid());
            transitSendVO.setType(TransitSendConstants.type_teacher);
            transitSendService.add(transitSendVO);
        }
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String generateNotice(TransitVO transitVO) {
        Transit transit = transitService.getById(transitVO.getUid());
        if(transit == null){
            throw new RuntimeException("出校信息不能为空");
        }
        String[] pushType = transit.getPushType().split(",");
        for (String type : pushType) {
            switch (Integer.parseInt(type)){
                case ETransit.PUSH_TEACHER://教师
                    GradeTeacherR teacher = gradeTeacherRService.getGradeTeacherByEmployeeUid(transit.getEmployeeUid(), 0);
                    List<TeacherAdminRVO> teacherAdminList = teacherAdminRService.getAdminByTeacherUid(teacher.getEmployeeUid());
                    for (TeacherAdminRVO teacherAdminRVO : teacherAdminList) {
                        TransitSendVO transitSendVO = new TransitSendVO();
                        transitSendVO.setTransitUid(transit.getUid());
                        transitSendVO.setAdminUid(teacherAdminRVO.getAdminUid());
                        transitSendVO.setType(Integer.parseInt(type));
                        transitSendService.add(transitSendVO);
                    }
                    break;
                case ETransit.PUSH_PARENTS://家长
                    List<UserEmployeeRVO> userEmployeerList = userEmployeeRService.getByEmployeeUid(transit.getEmployeeUid(), EUser.USER_EMPLOYEE_PARENTS);
                    for (UserEmployeeRVO userEmployeeRVO : userEmployeerList) {
                        TransitSendVO transitSendVO = new TransitSendVO();
                        transitSendVO.setTransitUid(transit.getUid());
                        transitSendVO.setUserUid(userEmployeeRVO.getUserUid());
                        transitSendVO.setType(Integer.parseInt(type));
                        transitSendService.add(transitSendVO);
                    }
                    break;
            }
        }
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    /**
     * 获取出入校总览
     *
     * @param transitVO
     * @return
     */
    @Override
    public TransitVO getSchoolEntryOverview(TransitVO transitVO) {
        return transitMapper.getSchoolEntryOverview(transitVO);
    }

    @Override
    public List<TransitVO> getGradeEntryOverview(TransitVO transitVO) {
        return transitMapper.getGradeEntryOverview(transitVO);
    }

    @Override
    public IPage<TransitVO> getGradeEntryRecord(TransitVO transitVO) {
        Page<Transit> page = new Page<>();
        page.setCurrent(transitVO.getCurrentPage());
        page.setSize(transitVO.getPageSize());
        IPage<TransitVO> pageList = transitMapper.getGradeEntryRecord(page, transitVO);
        List<TransitVO> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public IPage<TransitVO> getAllOutOfSchoolList(TransitVO transitVO) {
        Page<Transit> page = new Page<>();
        page.setCurrent(transitVO.getCurrentPage());
        page.setSize(transitVO.getPageSize());
        IPage<TransitVO> pageList = transitMapper.getAllOutOfSchoolList(page,transitVO);
        List<TransitVO> list = pageList.getRecords();
        for (TransitVO transit : list) {
            transit.setTypeName(TransitConstants.getTypeName(transit.getType()));
            transit.setStatusName(TransitConstants.getStatusName(transit.getType(), transit.getStatus()));
        }
        pageList.setRecords(list);
        return pageList;
    }
}
